#include "StdAfx.h"
#include "xfRefEdge.h"

xfRefEdge::xfRefEdge(void)
{
	memset(m_szEdgeFile,0,sizeof(m_szEdgeFile));
	ClearEdge();
}

xfRefEdge::~xfRefEdge(void)
{
}



int xfRefEdge::ClearEdge()
{
    memset(&m_xEdgeHead,0,sizeof(m_xEdgeHead));
	m_xBuffer.MemSet();
	m_fEdgeThreshold = 0.0f;

	return 0;
}


int xfRefEdge::AddAdjEdge(zkImgBitInfo *pImgInfo,DWORD dwImgBinary)
{
	if( NULL == pImgInfo )
	{
		return DD_ERR_PARAMETER;
	}



	return 0;
}

int xfRefEdge::AddSubEdge(zkImgBitInfo *pImgInfo,DWORD dwMaxRed,DWORD dwMinBlue)
{
	if( NULL == pImgInfo || dwMinBlue <= dwMaxRed )
	{
		return DD_ERR_PARAMETER;
	}


	return 0;
}


int xfRefEdge::SetEdgeConditionThreshold(float fThreshold)
{
	if( fThreshold<0 || fThreshold > 100 )
	{
		fThreshold = 10;
	}

	m_fEdgeThreshold           = fThreshold;
	m_xEdgeHead.fEdgeThreshold = fThreshold;

	SaveEdge();

	return 0;
}


#include "DDReadWriteFile.h"

int xfRefEdge::SetEdgeFile(char *pFileName)
{
	strcpy(m_szEdgeFile,pFileName);

	return 0;
}

int xfRefEdge::LoadEdge()
{
	int nRet = -1;

	DDReadWriteFile xFile;
    zkAnalyEdgeHead *pHead = &m_xEdgeHead;

	do 
	{
		char *pFileName = m_szEdgeFile;


		char xBuffer[1024]={0};

		BITMAPFILEHEADER *pBitFileHead = (BITMAPFILEHEADER*)xBuffer;
		BITMAPINFOHEADER *pBitInfoHead = (BITMAPINFOHEADER*)( xBuffer + sizeof(BITMAPFILEHEADER) );

		LONGLONG llFileSize = 0;
		if( 0 != xFile.DDCreateFile(pFileName,TRUE,&llFileSize) )
		{
			nRet = -1;
			break;
		}
		LONGLONG llFileOffset = 0;
		nRet = xFile.DDOperatorFile( (BYTE*)xBuffer,sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		DWORD dwWidth  = pBitInfoHead->biWidth;
		DWORD dwHeight = pBitInfoHead->biHeight;
		DWORD dwSize   = dwWidth * dwHeight;
		if( !m_xBuffer.Allocate(dwSize) )
		{
			nRet = DD_ERR_NEWBUFFER;
			break;
		}
        m_xBuffer.MemSet();
		BYTE *pEdges1 = (BYTE*)m_xBuffer.pBuffer;
		BYTE *pEdges  = (BYTE*)m_xBuffer.GetIdleBuffer();

		llFileOffset = pBitFileHead->bfOffBits;

		nRet = xFile.DDOperatorFile( pEdges1,dwSize,&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		for(DWORD i=0;i<dwHeight;i++)
		{
			memcpy( pEdges+i*dwWidth, pEdges1+(dwHeight-i-1)*dwWidth, dwWidth );
		}

		llFileOffset = llFileSize - 4*sizeof(DWORD);
		nRet = xFile.DDOperatorFile( (BYTE*)&(pHead->dwRed),sizeof(DWORD),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}
		nRet = xFile.DDOperatorFile( (BYTE*)&(pHead->dwGreen),sizeof(DWORD),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}
		nRet = xFile.DDOperatorFile( (BYTE*)&(pHead->dwRedAdjust),sizeof(DWORD),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}
		nRet = xFile.DDOperatorFile( (BYTE*)&(pHead->dwGreenAdjust),sizeof(DWORD),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		m_fEdgeThreshold = 1.0f;

		pHead->dwWidth  = dwWidth;
		pHead->dwHeight = dwHeight;
		pHead->pEdges   = pEdges;

		return nRet;

	} while (FALSE);

	pHead->pEdges = NULL;
	memset(pHead,0,sizeof(zkAnalyEdgeHead));

	return nRet;
}

int xfRefEdge::SaveEdge()
{
	return 0;
}